import { yapiItemsToApiItems } from '@/entrypoints/home/utils/importer/yapiToApiItems'
import { IMPORT_FROM_YAPI_PAGE_KEY, store } from '@/utils/store/store'

// 从Yapi页面中点击的导入，则检查存储，是否有数据
export const checkExistImFromYapiPage = async () => {
    const data = await store.get(IMPORT_FROM_YAPI_PAGE_KEY)
    const { apiList } = data || {}
    if (!data) {
        return false
    }
    if (!apiList?.length) {
        await store.remove(IMPORT_FROM_YAPI_PAGE_KEY)
        return false
    }
    return true
}

// 获取数据
export const imFromYapiPageDataToApiItems = async () => {
    const data = await store.get(IMPORT_FROM_YAPI_PAGE_KEY)
    const { apiList, recommendGroupName } = data || {}
    if (!data) {
        return
    }
    await store.remove(IMPORT_FROM_YAPI_PAGE_KEY)
    if (!apiList?.length) {
        return
    }
    const apiItems = await yapiItemsToApiItems(apiList, {
        mode: ImportMode.yapiPage,
    })
    return {
        apiItems,
        recommendGroupName,
    }
}
